import { orderRevertState, onSiteVisitTimeVerification } from '@/api/order'
import { ref } from 'vue'

/**
 * 工单回退到上一个状态
 */
export const useOrderRevertStateService = () => {
  const revertStateLoading = ref(false)

  const revertState = async (orderId: string) => {
    try {
      revertStateLoading.value = true
      const res = await orderRevertState(orderId)
      if (res.data.code === 20000) {
        return ''
      }

      return res.data.message
    } catch (error) {
      return '驳回失败'
    } finally {
      revertStateLoading.value = false
    }
  }

  return {
    revertStateLoading,
    revertState,
  }
}

/**
 * 核实上门时间服务
 */
export const useOnSiteVisitTimeVerificationService = () => {
  const verificationLoading = ref(false)
  const verificationForm = ref({
    remark: '',
    verifiedVisitTime: '',
    workId: '',
  })

  const resetVerificationForm = () => {
    verificationForm.value = {
      remark: '',
      verifiedVisitTime: '',
      workId: '',
    }
  }

  const save = async () => {
    try {
      verificationLoading.value = true
      const res = await onSiteVisitTimeVerification(verificationForm.value)
      verificationLoading.value = false
      return res.data
    } catch (error) {
      verificationLoading.value = false
    }
  }

  return {
    verificationForm,
    verificationLoading,
    save,
    resetVerificationForm,
  }
}
